<?php
namespace BP\DA;
require_once("java/util/ArrayList.php");
require_once("java/util/List.php");
require_once("java/util/Map.php");
require_once("BP/Tools/StringHelper.php");
class DataTable //implements Cloneable 
{
	public $Rows;	// DataRowCollection
	public $Columns;	// DataColumnCollection
	public $TableName;	// String
	protected $dataRows;	// DataRow[]
	public function copy () 
	{
		$v = DataTable::constructor__();
		foreach ($this->Columns as $Column) 		{
			$v->Columns->Add($Column->ColumnName, $Column->DataType);
		}
		return $v;
	}
	public function clone () 
	{
		try 
		{
// 			$v = parent->clone();
			$v->Rows = $this->Rows->clone();
			$v->Columns = $this->Columns->clone();
			return $v;
		}
		catch (\Exception $e)
		{
		    throw new \Exception();
		}
	}
	public static function constructor__ () 
	{
		$me = new self();
		$me->Columns = DataColumnCollection::constructor__DataTable($me);
		$me->Rows = DataRowCollection::constructor__DataTable($me);
		return $me;
	}
	public static function constructor__String ($tableName) // [String tableName]
	{
		$me = new self();
		parent::constructor__();
		$me->TableName = $tableName;
		return $me;
	}
	public function NewRow () 
	{
		$row = DataRow::constructor__DataTable($this);
		return $row;
	}
	public function setValue_I_I_Object ($rowIndex, $columnIndex, $value) // [int rowIndex, int columnIndex, Object value]
	{
		$this->Rows->get($rowIndex)->setValue($columnIndex, $value);
	}
	public function setValue_I_String_Object ($rowIndex, $columnName, $value) // [int rowIndex, String columnName, Object value]
	{
		$this->Rows->get($rowIndex)->setValue($columnName->toLowerCase(), $value);
	}
	public function getValue_I_I ($rowIndex, $columnIndex) // [int rowIndex, int columnIndex]
	{
		return $this->Rows->get($rowIndex)->getValue($columnIndex);
	}
	public function Clear () 
	{
		$this->Rows->clear();
		$this->Columns->clear();
	}
	public function ClearRow () 
	{
		$this->Rows->clear();
	}
	public function Select_Map ($filterMap) // [Map<String, Object> filterMap]
	{
		$dataRowList = []; //new ArrayList();
		/* unprocessed statement [japa.parser.ast.stmt.LabeledStmt] :*/   
		outer: 
		for (int i = 0; i < Rows.size(); i++) {
    DataRow row = Rows.get(i);
    for (Object key : filterMap.keySet().toArray()) {
        Object lefteql = filterMap.get(key);
        Object righteql = row.getValue(key.toString());
        if (lefteql == null || "".equals(lefteql)) {
            if (righteql == null || "".equals(righteql)) {
                continue;
            } else {
                continue outer;
            }
        } else if (!filterMap.get(key).toString().toUpperCase().equals(row.getValue(key.toString()).toString().toUpperCase())) {
            continue outer;
        }
    }
    dataRowList.add(row);
}
		return $dataRowList;
	}
	public function select ($filterString) // [String filterString]
	{
		$rows = []; //new ArrayList();
		if (!(is_null($filterString) || $filterString == ''))
		{
			$bl = null;
			foreach ($this->Rows as $row) 			{
				$currentRow = $row;
				try 
				{
					$bl = $this->dataRowCompute($filterString, $currentRow);
				}
				catch (\Exception $e)
				{
					$System->err->println("语法错误");
					$e->getMessage(); // 
					continue;
				}
				if ($bl)
				{
					$rows->add($currentRow);
				}
			}
			return $rows;
		}
		else
		{
			return $this->Rows;
		}
	}
	public function selectx ($filterString) // [String filterString]
	{
		$rows = []; //new ArrayList();
		if (!(is_null($filterString) || $filterString == ''))
		{
			$bl =  FALSE ;
			foreach ($this->Rows as $row) 			{
				$currentRow = $row;
				try 
				{
				    if ((count(explode('=', $filterString, -1)) /*from: filterString.split("=").length*/ > 1))
				        $bl = $currentRow->getValue_DataColumn(explode('=', $filterString, -1)[0]->trim())->equals($filterString->split("=")[1]->trim());
				}
				catch (\Exception $e)
				{
					$System->err->println("语法错误");
					$e->getMessage(); // 
					continue;
				}
				if ($bl)
				{
					$rows->add($currentRow);
				}
			}
			return $rows;
		}
		else
		{
			return $rows;
		}
	}
	protected function dataRowCompute ($filterString, $row) // [String filterString, DataRow row]
	{
		if ((($filterString . NULL) . ($row == NULL)))
		{
			return  FALSE ;
		}
		$orResult =  FALSE ;
		try 
		{
			$or = $filterString->split(" (?i)or ");
			foreach ($or as $o) 			{
				$andResult =  TRUE ;
				$and = $o->split(" (?i)and ");
				foreach ($and as $a) 				{
					$kv = NULL;
					if ($a->contains(">="))
					{
						$kv = $a->split(">=");
					}
					else
						if ($a->contains(">"))
						{
							$kv = $a->split(">");
						}
						else
							if ($a->contains("<="))
							{
								$kv = $a->split("<=");
							}
							else
								if ($a->contains("<"))
								{
									$kv = $a->split("<");
								}
								else
									if ($a->contains("!="))
									{
										$kv = $a->split("!=");
									}
									else
										if ($a->contains("="))
										{
											$kv = $a->split("=");
										}
					if ((($kv != NULL) && (count($kv) /*from: kv.length*/ == 2)))
					{
						$key = $kv[0]->trim()$value = $kv[1]->trim();
						if ((($key . NULL) . ($value . NULL)))
						{
							$val = $row->getValue_String($key);
							if ($value->equalsIgnoreCase("is null"))
							{
								if (!(($val == NULL)))
								{
									$andResult =  FALSE ;
									break;
								}
							}
							else
								if ($value->equalsIgnoreCase("is not null"))
								{
									if (!(($val != NULL)))
									{
										$andResult =  FALSE ;
										break;
									}
								}
								else
									if (($value->startsWith("'") && $value->endsWith("'")))
									{
										$v = $value->replaceAll("'", "");
										if ($a->contains("!="))
										{
											if (!(!$val->equalsIgnoreCase($v)))
											{
												$andResult =  FALSE ;
												break;
											}
										}
										else
											if ($a->contains("="))
											{
												if (!($val->equalsIgnoreCase($v)))
												{
													$andResult =  FALSE ;
													break;
												}
											}
									}
									else
									{
										$v = $Integer->valueOf($value);
										$dbVal = $Integer->valueOf($val);
										if ($a->contains(">="))
										{
											if (!(($dbVal >= $v)))
											{
												$andResult =  FALSE ;
												break;
											}
										}
										else
											if ($a->contains(">"))
											{
												if (!(($dbVal > $v)))
												{
													$andResult =  FALSE ;
													break;
												}
											}
											else
												if ($a->contains("<="))
												{
													if (!(($dbVal <= $v)))
													{
														$andResult =  FALSE ;
														break;
													}
												}
												else
													if ($a->contains("<"))
													{
														if (!(($dbVal < $v)))
														{
															$andResult =  FALSE ;
															break;
														}
													}
													else
														if ($a->contains("!="))
														{
															if (!(($dbVal != $v)))
															{
																$andResult =  FALSE ;
																break;
															}
														}
														else
															if ($a->contains("="))
															{
																if (!(($dbVal == $v)))
																{
																	$andResult =  FALSE ;
																	break;
																}
															}
									}
						}
					}
				}
				if ($andResult)
				{
					$orResult =  TRUE ;
					break;
				}
			}
		}
		catch (\Exception $e)
		{
			$System->err->println("语法错误");
			$e->getMessage(); // 
		}
		return $orResult;
	}
	public function getTableName () 
	{
		return $this->TableName;
	}
	public function setTableName ($tableName) // [String tableName]
	{
		$this->TableName = $tableName;
	}
	public function getValue_I_String ($rowindex, $columnName) // [int rowindex, String columnName]
	{
		return $this->Rows->get($rowindex)->getValue($columnName);
	}
	public function Select_String ($string) // [String string]
	{
		$dataRowsx = NULL;
		$dataRowList = []; //new ArrayList();
		if (((is_null($string) || $string == '')) ==  FALSE ))
		{
			$bl = null;
			foreach ($this->Rows as $row) 			{
				$currentRow = $row;
				try 
				{
					$bl = $this->dataRowCompute($string, $currentRow);
				}
				catch (\Exception $e)
				{
					$System->err->println("语法错误");
					$e->getMessage(); // 
					continue;
				}
				if ($bl)
				{
					$dataRowList->add($currentRow);
				}
			}
			$dataRowsx = array();
			for ($i = 0; ($i < $dataRowList->size()); ++$i) 
			{
				$dataRowsx[$i] = $dataRowList->get($i);
			}
			return $dataRowsx;
		}
		else
		{
			return $dataRowsx;
		}
	}
}
?>
